/*\
 * Copyright 2012 Rob van de Werken
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
\*/

package nl.RvdW.TVgids;

import android.os.Bundle;
//import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.HorizontalScrollView;
import android.widget.TabHost;

public class TVgidsActivity extends FragmentActivity implements
		TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// Default initialisation
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		//// Fix ICS policy for urlConnection
		//StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
	    //StrictMode.setThreadPolicy(policy);
		
		// Initialize globals
		Global.context = this;
		Global.pageradapter = new PagerAdapter(
				super.getSupportFragmentManager(), Global.fragments);
		Global.viewpager = (ViewPager) super.findViewById(R.id.tabviewpager);
		Global.viewpager.setAdapter(Global.pageradapter);
		Global.viewpager.setOnPageChangeListener(this);
		Global.tabs = (TabHost) findViewById(android.R.id.tabhost);
		Global.tabs.setup();
		Global.tabs.setOnTabChangedListener(this);
		Global.db = new DatabaseHelper(this);

		// TODO: Check database age and update if getting old.
		if (savedInstanceState != null) {
			// set the tab as per the saved state
			Global.tabs.setCurrentTabByTag(savedInstanceState.getString("tab"));
		}
	}

	/** Add menu-button functionality */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	/** Handle menu-button actions */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menu_update:
			Global.db.update();
			return true;
		case R.id.menu_configure:
			//Show configuration etc.
			return true;
		case R.id.menu_exit:
			finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	/** Called when the activity is resumed. */
	@Override
	public void onStart() {
		super.onStart();
		// Connect database
		Global.db.open();
	}

	/** Called when the activity is paused. */
	@Override
	public void onStop() {
		super.onStop();
		// Disconnect database
		Global.db.close();
	}

	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
	}

	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
	}

	public void onPageSelected(int position) {
		// TODO Auto-generated method stub
		Global.tabs.setCurrentTab(position);
		
		HorizontalScrollView hScrollView = (HorizontalScrollView)findViewById(R.id.scroller);
	    hScrollView.scrollTo(Global.tabs.getCurrentTabView().getLeft()+(Global.tabs.getCurrentTabView().getWidth()/2)-(getWindowManager().getDefaultDisplay().getWidth()/2),0);
	}

	public void onTabChanged(String arg0) {
		// TODO Auto-generated method stub
		int pos = Global.tabs.getCurrentTab();
		Global.viewpager.setCurrentItem(pos);
	}
}